<?php
namespace app\admin\controller;
use app\admin\model\User;
use think\Controller;
use think\Session;
//后台登陆类
class Login extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->user = new User();
    }
    //登陆页面
    public function login(){

        return $this->fetch();
    }
    //登录验证
    public function loginCheck(){
        $user_name = request()->post('userName','');
        $password = request()->post('password','');
        $vertification = request()->post('vertification','');
        if (! captcha_check($vertification)) {
            $retval = [
                'code' => 0,
                'message' => "验证码错误"
            ];
            return $retval;
        }
        $retval = $this->user->login($user_name, $password);
        if($retval == -2){
            $retval = [
                'code' => 0,
                'message' => "没有该管理员"
            ];
        }elseif($retval == -1){
            $retval = [
                'code' => 0,
                'message' => "密码错误"
            ];
        }elseif($retval == -3){
            $retval = [
                'code' => 0,
                'message' => "用户锁定"
            ];
        }elseif($retval == -4){
            $retval = [
                'code' => 0,
                'message' => "没有任何权限"
            ];
        }
        else{
            $retval = [
                'code' => 1,
                'message' => "登陆成功"
            ];
        }
        return $retval;
    }
    //用户退出登录
    public function logout(){
        $adminId = Session::set("adminId",null);
        if(empty($adminId)){
            return $this->fetch('login/login');
        }
    }
}
